package com.epfly.promo.activity.impl.discount.predicate;

import com.epfly.iface.promo.activity.enums.ActivityRuleDinnerTypeEnum;
import com.epfly.iface.promo.activity.enums.DiscountTypeEnum;
import com.epfly.promo.activity.dao.ActivityRuleMapper;
import com.epfly.promo.activity.entity.ActivityEntity;
import com.epfly.promo.activity.entity.ActivityRuleEntity;
import com.epfly.promo.activity.feign.DataRestaurantFeignService;
import com.epfly.promo.activity.impl.discount.ActivityWrapper;
import org.springframework.core.annotation.Order;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.Arrays;
import java.util.List;
import java.util.Objects;

/**
 * 餐别过滤
 */
@Order(value = PredicateOrder.DINNER_TYPE_ORDER)
@Service
public class DinnerTypePredicate implements ActivityDiscountPredicate {

    @Resource
    private DataRestaurantFeignService dataRestaurantFeignService;
    @Resource
    private ActivityRuleMapper ruleMapper;

    @Override
    public boolean test(ActivityWrapper activityWrapper) {

        ActivityEntity activityEntity = activityWrapper.getActivityEntity();
        // 特价菜不考虑餐别
        if (activityEntity.getDiscountType().equals(DiscountTypeEnum.FOOD_BARGAIN.getKey())) {
            return true;
        }

        Integer dinnerType = dataRestaurantFeignService.getMealType(activityWrapper.getMerchantId(), activityWrapper.getStoreId(), activityWrapper.getOrderTime());

        // note: 查"餐别规则体"的时候需加上"不限".这样只要查到的结果里面有值，即可。
        List<Byte> dinnerTypeList = Arrays.asList(dinnerType.byteValue(), ActivityRuleDinnerTypeEnum.UN_LIMIT.getKey());
        ActivityRuleEntity ruleEntity = ruleMapper.existsByActivityIdAndDinnerTypeList(activityEntity.getId(), dinnerTypeList);

        return Objects.nonNull(ruleEntity);
    }
}
